// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

var NetworkManager = require('NetworkManager')
var ClickManager = require('ClickManager')

var scoreDetailsItem = cc.Class({
    name: 'scoreDetailsItem',
    properties: {
        username: 0,
        win: 0
    }
});

cc.Class({
    extends: cc.Component,

    properties: {
        item: {
            default: [],
            type: scoreDetailsItem
        },
        itemPrefab: cc.Prefab,
        scoreDetailsNode: cc.Node
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {

    },

    start () {

    },

    onEnable () {
        this.getDetail()
    },

    getDetail () {
        var tid = cc.sys.localStorage.getItem('__DTID__')
        var innings = cc.sys.localStorage.getItem('__DINNINGS__')
        this.scoreDetailsNode.removeAllChildren()

        try {
            NetworkManager.onConnectedToHall( (hallService) => {
                hallService.proxy.getScoreDetails(tid, innings, (data) => {
                    var list = JSON.parse(data.datas)
                    cc.log('战绩数据:', list)
                    this.scoreDetailsNode.height = list.length + 1 + 50

                    let obj = {
                        username: '昵称',
                        win: '输赢'
                    }
                    var item = cc.instantiate(this.itemPrefab)
                    this.scoreDetailsNode.addChild(item)
                    item.getComponent('scoreDetailsItemList').init(obj)

                    for (let i in list) {
                        let obj = {
                            username: list[i].username,
                            win: list[i].win
                        }
                        if (obj.win > 0) obj.win = obj.win

                        var item = cc.instantiate(this.itemPrefab)
                        this.scoreDetailsNode.addChild(item)
                        item.getComponent('scoreDetailsItemList').init(obj)
                    }
                })
            })
        } catch (err) {
            console.log('异常处理，待处理:', err)
        }
    }

    // update (dt) {},
});
